package lanou.giftsay_01.fragment.body;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextClock;
import android.widget.Toast;

import com.youth.banner.Banner;

import java.util.ArrayList;

import lanou.giftsay_01.adapter.MyLvadapter;
import lanou.giftsay_01.adapter.MyRvAdapter;
import lanou.giftsay_01.adapter.VpAdapter;
import lanou.giftsay_01.bean.GuideBean;
import lanou.giftsay_01.bean.RollBean;
import lanou.giftsay_01.bean.ScrollBean;
import lanou.giftsay_01.main.hotmain.HotSecondActivity;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;
import lanou.giftsay_01.R;
import lanou.giftsay_01.thread.SleepThread ;


/**
 * Created by dllo on 16/8/15.
 */
public class SelectFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private String TAG = "ddd";
    private MyLvadapter myLvadapter;
    private ListView lv;
    private ImageView firstIv;
    private ImageView secondIv;
    private ImageView thirdIv;
    private ImageView fourceIv;
    private VpAdapter vpAdapter;
    private ViewPager vp;
    private ArrayList<View> arrayList;
    private View view1;
    private View view2;
    private View view3;
    private View view4;
    private Handler handler;
    private View view;
    private RecyclerView recyclerView ;
    private MyRvAdapter myRvAdapter;
    private Thread thread;
    private SleepThread sleepThread;
    private SwipeRefreshLayout swipeRefreshLayout;
    private NetTool netTool;
    private Banner banner;

    @Override
    protected int setLayout() {
        return R.layout.select_fragment;
    }
    @Override
    protected void initView() {
        banner = (Banner) getView().findViewById(R.id.select_banner);
        lv = (ListView) getView().findViewById(R.id.select_lv);
        view = LayoutInflater.from(mContext).inflate(R.layout.guide_head_viewpager, null );
        //vp = (ViewPager) view.findViewById(R.id.guide_head_vp);
        //vpAdapter = new VpAdapter(mContext);
        myLvadapter = new MyLvadapter(mContext);
        recyclerView = (RecyclerView) view.findViewById(R.id.select_head_rv);
        GridLayoutManager manager = new GridLayoutManager(mContext , 1);
        recyclerView.setLayoutManager(manager);
        manager.setOrientation(GridLayoutManager.HORIZONTAL);
        myRvAdapter = new MyRvAdapter(mContext);
        swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_container);
    }

    @Override
    protected void initData() {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright);   // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);// 设置圆圈的大小
        swipeRefreshLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this);
        /**
         * 轮播图的数据
         */
        netTool = new NetTool();
        netTool.getNetData(URLValues.CAROUSEL, RollBean.class, new NetTool.NetListener<RollBean>() {
            @Override
            public void onSuccess(RollBean rollBean) {
                String [] url = new String[rollBean.getData().getBanners().size()];
                for (int i = 0; i < rollBean.getData().getBanners().size(); i++) {
                     url[i] = rollBean.getData().getBanners().get(i).getImage_url();
                }
                   initBanner(url);
            }
            @Override
            public void onError(String errorMsg) {
                Toast.makeText(mContext, "数据解析失败", Toast.LENGTH_SHORT).show();
            }
        });
//        vpAdapter.setViewPager(vp);
//        vp.setCurrentItem(3000 , false);
//        handler = new Handler(new Handler.Callback() {
//            @Override
//            public boolean handleMessage(Message msg) {
//                int  currentItem = msg.what ;
//                vp.setCurrentItem(currentItem + 1);
//
//                return false;
//            }
//        });
        lv.addHeaderView(view);
        /**
         * 小方框的数据
         */
        netTool.getNetData(URLValues.SPECIAL, ScrollBean.class, new NetTool.NetListener<ScrollBean>() {
            @Override
            public void onSuccess(ScrollBean scrollBean) {
                ArrayList<ScrollBean> arrayList = new ArrayList<>();
                arrayList.add(scrollBean);
                myRvAdapter.setArrayList(arrayList);
                recyclerView.setAdapter(myRvAdapter);
            }
            @Override
            public void onError(String errorMsg) {
                Toast.makeText(mContext, "数据加载失败", Toast.LENGTH_SHORT).show();

            }
        });

        /**
         * listview的数据
         */
        netTool.getNetData(URLValues.SELECT, GuideBean.class, new NetTool.NetListener<GuideBean>() {
            @Override
            public void onSuccess(GuideBean guideBean) {
                ArrayList<GuideBean> arrayList = new ArrayList<GuideBean>();
                ArrayList<Boolean> checkedArrayList = new ArrayList<Boolean>();
                arrayList.add(guideBean);
                for (int i = 0; i < guideBean.getData().getItems().size() ; i++) {
                    checkedArrayList.add(false);
                }
                myLvadapter.setChecked(checkedArrayList);
                myLvadapter.setArrayList(arrayList);

                lv.setAdapter(myLvadapter);

                /**
                 * 设置监听,接口回调
                 */
                myLvadapter.setOnClickGuideItem(new OnClickGuideItem() {
                    @Override
                    public void onClick(int position, ArrayList<GuideBean> arrayList) {
                        Intent intent = new Intent(SelectFragment.this.getContext(), HotSecondActivity.class);
                        intent.putExtra("key" , arrayList.get(0).getData().getItems().get(position).getUrl());
                        startActivity(intent);

                    }
                });
            }
            @Override
            public void onError(String errorMsg) {

            }
        });


    }

    /**
     * 开启轮播图线程睡眠
     */
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called with: " + "");
        sleepThread = new SleepThread( handler ,vp );
        thread = new Thread(sleepThread);
        thread.start();
    }

    /**
     * 停止线程睡眠
     */
    @Override
    public void onStop() {
       // handler.removeMessages();
        super.onStop();
        Log.d(TAG, "onStop() called with: " + "");
        sleepThread.setFlag(false);
    }


    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 停止刷新
                swipeRefreshLayout.setRefreshing(false);
        netTool.getNetData(URLValues.CAROUSEL, RollBean.class, new NetTool.NetListener<RollBean>() {
            @Override
            public void onSuccess(RollBean rollBean) {
                ArrayList<RollBean> arrayList = new ArrayList<>();
                arrayList.add(rollBean);
                vpAdapter.setArrayList(arrayList);

            }
            @Override
            public void onError(String errorMsg) {

            }
        });
        netTool.getNetData(URLValues.SPECIAL, ScrollBean.class, new NetTool.NetListener<ScrollBean>() {
            @Override
            public void onSuccess(ScrollBean scrollBean) {
                ArrayList<ScrollBean> arrayList = new ArrayList<>();
                arrayList.add(scrollBean);
                myRvAdapter.setArrayList(arrayList);

            }
            @Override
            public void onError(String errorMsg) {
            }
        });
        netTool.getNetData(URLValues.SELECT, GuideBean.class, new NetTool.NetListener<GuideBean>() {
            @Override
            public void onSuccess(GuideBean guideBean) {
                ArrayList<GuideBean> arrayList = new ArrayList<GuideBean>();
                arrayList.add(guideBean);
                myLvadapter.setArrayList(arrayList);

            }
            @Override
            public void onError(String errorMsg) {
            }
        });
                Toast.makeText(mContext, "刷新成功", Toast.LENGTH_SHORT).show();
            }
        }, 5); // 5秒后发送消息，停止刷新
    }
    private void initBanner(String[] url) {
        //设置小圆点

        banner.setBannerStyle(Banner.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
        //设置位置居中
        banner.setIndicatorGravity(Banner.TEXT_ALIGNMENT_CENTER);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置图片
        banner.setImages(url);

    }
}
